[code-simplifier] refactor: simplify nested ternary and duplicate assignments in recent changes#9477
Conversation
… changes - MSTestSettings.Configuration.cs: replace nested ternary chain for parallelism:workers parsing with an if/else chain; project guidelines explicitly forbid nested ternaries (prefer if/else or switch). Behavior is identical: parse failure or negative value throws, 0 maps to ProcessorCount, positive value is used as-is. - VideoRecorderSessionHandler.cs: eliminate duplicate _persistMode / _granularity assignments in the constructor. Both branches set the same fields from options; extracted them to a shared assignment after the conditional ApplyCommandLineOverrides call. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR performs small refactors in two recently changed areas to improve readability and reduce duplication while preserving existing behavior in MSTest adapter configuration parsing and the VideoRecorder MTP extension.
Changes:
- Replaced a nested ternary chain with an explicit parse/validate
ifguard formstest:parallelism:workers. - Removed duplicate
_persistMode/_granularityassignments inVideoRecorderSessionHandlerby centralizing them after the conditional override logic.
Show a summary per file
| File | Description |
|---|---|
| src/Adapter/MSTestAdapter.PlatformServices/MSTestSettings.Configuration.cs | Simplifies parallelism:workers parsing while preserving: parse failure/negative ⇒ throw; 0 ⇒ Environment.ProcessorCount; positive ⇒ value. |
| src/Platform/Microsoft.Testing.Extensions.VideoRecorder/VideoRecorderSessionHandler.cs | Avoids duplicated field assignments by applying CLI overrides only when enabled, then assigning _persistMode/_granularity once before the disabled early-return. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Low
Evangelink
left a comment
There was a problem hiding this comment.
Note
🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.
✅ 22/22 dimensions clean — no findings.
Behavior-equivalence trace
MSTestSettings.Configuration.cs
The original nested ternary had four execution paths; the refactored guard+ternary covers them identically:
| Input | Original | Refactored |
|---|---|---|
int.TryParse fails |
outer throw |
!int.TryParse(...) → throw |
parallelWorkers < 0 |
innermost throw |
parallelWorkers < 0 → throw |
parallelWorkers == 0 |
Environment.ProcessorCount |
ternary true-branch |
parallelWorkers > 0 |
parallelWorkers |
ternary false-branch |
Both throw the same AdapterSettingsException with the same message format for both invalid cases. Existing tests GetSettingsShouldThrowIfParallelizationWorkersIsNotInt (line 409), GetSettingsShouldThrowIfParallelizationWorkersIsNegative (line 428), ParallelizationWorkersShouldBeConsumedFromRunSettingsWhenSpecified (line 446), and ParallelizationWorkersShouldBeSetToProcessorCountWhenSetToZero (line 464) in MSTestSettingsTests.cs exercise every branch.
VideoRecorderSessionHandler.cs
ApplyCommandLineOverrides mutates options.PersistMode and options.Granularity based on CLI arguments. Both the old and new code ensure the assignments _persistMode = options.PersistMode and _granularity = options.Granularity happen after the call when _enabled == true, preserving the ordering invariant. When _enabled == false neither version calls ApplyCommandLineOverrides, so the unmodified option defaults are captured in both cases.
N/A dimensions (8): 10 Test Isolation, 11 Assertion Quality, 12 Flakiness Patterns, 14 Data-Driven Coverage, 18 Analyzer Quality, 19 IPC Wire Compatibility, 20 Build Infrastructure, 22 PowerShell Scripting Hygiene.
|
This pull request was automatically closed because it expired on 2026-06-28T13:55:40.146Z.
|
Code Simplification — 2026-06-27
This PR simplifies recently modified code to improve clarity, consistency, and maintainability while preserving all functionality.
Files Simplified
src/Adapter/MSTestAdapter.PlatformServices/MSTestSettings.Configuration.cs— Replace nested ternary chain with an if/else chainsrc/Platform/Microsoft.Testing.Extensions.VideoRecorder/VideoRecorderSessionHandler.cs— Eliminate duplicate field assignments in constructorImprovements Made
1. Removed Nested Ternary (
MSTestSettings.Configuration.cs)The
parallelism:workersparsing inSetSettingsFromConfigused a nested ternary chain:The project guidelines explicitly state: "Avoid nested ternary operators — prefer switch statements or if/else chains." This file was modified by PR #9453 (merged 2026-06-26).
2. Eliminated Duplicate Assignments (
VideoRecorderSessionHandler.cs)The constructor (new file from PR #9377, merged 2026-06-26) set
_persistModeand_granularityin both the enabled and disabled branches:Changes Based On
Recent changes from:
timeout:*testconfig.json keys #9453 — Clarify and document 0-value behavior fortimeout:*testconfig.json keys (modifiedMSTestSettings.Configuration.cs)VideoRecorderSessionHandler.cs)Testing
MSTestAdapter.PlatformServicesbuilds cleanly (0 warnings, 0 errors) fornet8.0andnet9.0Microsoft.Testing.Extensions.VideoRecorderbuilds cleanly (0 warnings, 0 errors) fornet8.0,net9.0, andnetstandard2.0Review Focus
Please verify:
parallelism:workersvalidation (parse failure or negative → throw,0→ProcessorCount, positive → value)_recordercreation to when_enabledis trueAdd this agentic workflows to your repo
To install this agentic workflow, run